//
//  ModifyPassWordViewController.m
//  zhuchiren
//
//  Created by rainday on 15/1/11.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "ModifyPassWordViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "MeViewModel.h"

@interface ModifyPassWordViewController ()
@property (nonatomic ,weak) IBOutlet UIView *navView;
@property (nonatomic ,weak) IBOutlet UIButton *setPassWordButton;
@property (nonatomic ,weak) IBOutlet UITextField *oldPassword;
@property (nonatomic ,weak) IBOutlet UITextField *aNewPassword;
@property (nonatomic ,weak) IBOutlet UITextField *bNewPassword;



- (IBAction)dismissSelf:(id)sender;
@end

@implementation ModifyPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _navView.layer.shadowColor = [[UIColor colorWithRed:224.0/255 green:79.0/255 blue:95.0/255 alpha:1.0] CGColor];
    _navView.layer.shadowOffset = CGSizeMake(1.0, 1.0);
    _navView.layer.shadowOpacity = .3;
    
    
    _setPassWordButton.layer.cornerRadius = 5;
    _setPassWordButton.layer.masksToBounds = YES;
    
    RAC(_setPassWordButton,enabled) = [RACSignal combineLatest:@[_oldPassword.rac_textSignal,_aNewPassword.rac_textSignal,_bNewPassword.rac_textSignal] reduce:^id(NSString *oldPassword,NSString *aNewPassword,NSString *bNewPassword){
        return @( [oldPassword length]>0 && [aNewPassword isEqualToString:bNewPassword] && [aNewPassword length]>0);
        
        
    }];
    
    RAC(_setPassWordButton,backgroundColor) = [RACSignal combineLatest:@[_oldPassword.rac_textSignal,_aNewPassword.rac_textSignal,_bNewPassword.rac_textSignal] reduce:^id(NSString *oldPassword,NSString *aNewPassword,NSString *bNewPassword){
        return ([oldPassword length]>0 && [aNewPassword isEqualToString:bNewPassword] && [aNewPassword length]>0)?[UIColor redColor]:[UIColor lightGrayColor];
        
        
        
    }];
    
    
    @weakify(self);
    [[_setPassWordButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [[MeViewModel setPassword:_oldPassword.text aNewPassword:_aNewPassword.text] subscribeNext:^(id x) {
            @strongify(self);
            [self.delegate passWordViewController:self hasFinishedUpdate:YES];
            
        } error:^(NSError *error) {
            
        }];
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dismissSelf:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
